home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / misc / nogblank.lha / Blankers / Rival / blank.c next >
C/C++ Source or Header  |  1995-07-30  |  8KB  |  328 lines

  1. /*
  2.  *  Copyright (c) 1995  Andreas Schmitz
  3.  *  All rights reserved.
  4.  *
  5.  *  This is a blanker-module for Garshne-Blanker 3.6
  6.  *
  7.  *  Rival 38.1
  8.  *
  9.  */
  10.  
  11. #include <exec/memory.h>
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <time.h>
  15. #include <float.h>
  16. #include <stdlib.h>
  17. #include "/includes.h"
  18.  
  19. #define SQSIZE    0
  20. #define BLACKWIN  2
  21. #define RESTART   4
  22. #define TOPSCR    8
  23.  
  24. #define farb(i) ((i)*(0x01000000)) /* for setting RGB32-Colors */
  25.  
  26. #include "Rival_rev.h"
  27. STATIC const UBYTE VersTag[] = VERSTAG;
  28.  
  29. /* Routines */
  30.  
  31. /* alle Pixel im Rechteck in der gleichen Farbe ? */
  32. char Recheckt(struct RastPort *rp,int xmin,int ymin,int xmax,int ymax,int farbe)
  33. {
  34.    int breit,hoch;
  35.    int xmitt,ymitt;
  36.    int i,start,x,y;
  37.    int r[4][4];
  38.    char back=1;
  39.  
  40.    breit=xmax-xmin;
  41.    hoch=ymax-ymin;
  42.  
  43.    if ((breit<40)||(hoch<30)) {
  44.       for (y=ymin;y<=ymax;y++) {
  45.          for (x=xmin;x<=xmax;x++) {
  46.             if (ReadPixel(rp,x,y)!=farbe) {
  47.                return(0);
  48.             }
  49.          }
  50.       }
  51.       return(back);
  52.    }
  53.  
  54.    xmitt=xmin+1+(rand()%(breit-1));
  55.    ymitt=ymin+1+(rand()%(hoch-1));
  56.  
  57.    r[0][0]=xmin;
  58.    r[0][1]=ymin;
  59.    r[0][2]=xmitt;
  60.    r[0][3]=ymitt;
  61.  
  62.    r[1][0]=xmitt+1;
  63.    r[1][1]=ymitt+1;
  64.    r[1][2]=xmax;
  65.    r[1][3]=ymax;
  66.  
  67.    r[2][0]=xmitt+1;
  68.    r[2][1]=ymin;
  69.    r[2][2]=xmax;
  70.    r[2][3]=ymitt;
  71.  
  72.    r[3][0]=xmin;
  73.    r[3][1]=ymitt+1;
  74.    r[3][2]=xmitt;
  75.    r[3][3]=ymax;
  76.  
  77.    start=rand()%4;
  78.  
  79.    for (i=start+1;i<=3;i++) {
  80.       back=Recheckt(rp,r[i][0],r[i][1],r[i][2],r[i][3],farbe);
  81.       if (back==0) return(back);
  82.    }
  83.  
  84.    for (i=0;i<=start;i++) {
  85.       back=Recheckt(rp,r[i][0],r[i][1],r[i][2],r[i][3],farbe);
  86.       if (back==0) return(back);
  87.    }
  88.  
  89.    return(back);
  90. }
  91.  
  92. /* suche Pixel im Rechteck mit der Farbe und uebergebe Koordinaten */
  93. char ReSearch(struct RastPort *rp,int xmin,int ymin,int xmax,int ymax,int farbe,int *koord)
  94. {
  95.    int breit,hoch;
  96.    int xmitt,ymitt;
  97.    int i,start,x,y;
  98.    int xstart,dx,ystart,dy;
  99.    int r[4][4];
  100.    char back=0;
  101.  
  102.    breit=xmax-xmin;
  103.    hoch=ymax-ymin;
  104.  
  105.    if ((breit<80)||(hoch<60)) {
  106.       /* random search */
  107.       if (rand()%2==0) {
  108.          dx=4;
  109.          xstart=xmin;
  110.       }
  111.       else {
  112.          dx=-4;
  113.          xstart=xmax;
  114.       }
  115.       if (rand()%2==0) {
  116.          dy=3;
  117.          ystart=ymin;
  118.       }
  119.       else {
  120.          dy=-3;
  121.          ystart=ymax;
  122.       }
  123.       for (y=ystart;(y>=ymin)&&(y<=ymax);y=y+dy) {
  124.          for (x=xstart;(x>=xmin)&&(x<=xmax);x=x+dx) {
  125.             if (ReadPixel(rp,x,y)==farbe) {
  126.                koord[0]=x;
  127.                koord[1]=y;
  128.                return(1);
  129.             }
  130.          }
  131.       }
  132.       return(back);
  133.    }
  134.  
  135.    xmitt=xmin+1+(rand()%(breit-1));
  136.    ymitt=ymin+1+(rand()%(hoch-1));
  137.  
  138.    r[0][0]=xmin;
  139.    r[0][1]=ymin;
  140.    r[0][2]=xmitt;
  141.    r[0][3]=ymitt;
  142.  
  143.    r[1][0]=xmitt+1;
  144.    r[1][1]=ymitt+1;
  145.    r[1][2]=xmax;
  146.    r[1][3]=ymax;
  147.  
  148.    r[2][0]=xmitt+1;
  149.    r[2][1]=ymin;
  150.    r[2][2]=xmax;
  151.    r[2][3]=ymitt;
  152.  
  153.    r[3][0]=xmin;
  154.    r[3][1]=ymitt+1;
  155.    r[3][2]=xmitt;
  156.    r[3][3]=ymax;
  157.  
  158.    start=rand()%4;
  159.  
  160.    for (i=start+1;i<=3;i++) {
  161.       back=ReSearch(rp,r[i][0],r[i][1],r[i][2],r[i][3],farbe,koord);
  162.       if (back==1) return(back);
  163.    }
  164.  
  165.    for (i=0;i<=start;i++) {
  166.       back=ReSearch(rp,r[i][0],r[i][1],r[i][2],r[i][3],farbe,koord);
  167.       if (back==1) return(back);
  168.    }
  169.  
  170.    return(back);
  171. }
  172.  
  173. VOID Defaults( PrefObject *Prefs )
  174. {
  175.     Prefs[SQSIZE].po_Level = 12;
  176.     Prefs[BLACKWIN].po_Level = 0;
  177.     Prefs[RESTART].po_Active = 2;
  178.     Prefs[TOPSCR].po_Active = 0;
  179. }
  180.  
  181. LONG Blank( PrefObject *Prefs )
  182. {
  183.     struct Screen *Scr;
  184.    struct RastPort *RP;
  185.    struct ViewPort *VP;
  186.     struct Window *Wnd;
  187.  
  188.     LONG RetVal = OK;
  189.    LONG zeit,duration,Colors,black;
  190.     SHORT Width,Height,x,y,color;
  191.    SHORT xmin,xmax,ymin,ymax;
  192.    unsigned int Pixels,count=0;
  193.    int koord[2];
  194.    SHORT sqsize,blackwin;
  195.  
  196.     sqsize=Prefs[SQSIZE].po_Level;
  197.  
  198.    duration=999999999;
  199.    if (Prefs[RESTART].po_Active>0) duration=(4-Prefs[RESTART].po_Active)*30*CLK_TCK;
  200.  
  201.     blackwin=101-Prefs[BLACKWIN].po_Level;
  202.    if (blackwin==101) blackwin=0;
  203.    if (blackwin>90) blackwin=blackwin*2;
  204.    if (blackwin>80) blackwin=blackwin*2;
  205.    if (blackwin>60) blackwin=blackwin*2;
  206.  
  207.    srand((unsigned)clock());
  208.  
  209.    /* restart-loop */
  210.  
  211.    while(1) {
  212.  
  213.       Scr = cloneTopScreen( FALSE, Prefs[TOPSCR].po_Active );
  214.  
  215.       if (Scr) {
  216.          Wnd = BlankMousePointer( Scr );
  217.  
  218.          /* Get all Screen-Information */
  219.          VP=&((*Scr).ViewPort);
  220.          RP=&((*Scr).RastPort);
  221.                 Colors = 1L << Scr->RastPort.BitMap->Depth;
  222.          Width=Scr->Width;
  223.          Height=Scr->Height;
  224.          Pixels=Width*Height;
  225.  
  226.          /* Find the color that fits best to black
  227.             and change this color to real black (>=39)
  228.             Choose color no.1 for OS <39 */
  229.          if (blackwin>0) {
  230.             black=1;
  231.             if (GfxBase->lib_Version>38) {
  232.                black=FindColor(VP->ColorMap,farb(0),farb(0),farb(0),Colors-1);
  233.                if (black<0) black=1;
  234.                if (black>=Colors) black=1;
  235.             }
  236.             SetRGB32(VP,black,farb(0),farb(0),farb(0));
  237.          }
  238.  
  239.          zeit=clock();
  240.  
  241.          /* main-loop */
  242.          while(1) {
  243.  
  244.             count++;
  245.             x=rand()%Width;
  246.             y=rand()%Height;
  247.  
  248.             if ((blackwin>0)&&(count%blackwin==0)) {
  249.  
  250.                /* Try to find black systematicly */
  251.                /* (recursive ReSearch will do this for us) */
  252.  
  253.                color=black;
  254.                if (ReSearch(RP,0,0,Width-1,Height-1,color,koord)==1) {
  255.                   x=koord[0];
  256.                   y=koord[1];
  257.                }
  258.             }
  259.             else {
  260.                color=ReadPixel(RP,x,y);
  261.             }
  262.             SetAPen(RP,color);
  263.             xmin=x-sqsize;
  264.             ymin=y-sqsize;
  265.             if (xmin<0) xmin=0;
  266.             if (ymin<0) ymin=0;
  267.             xmax=x+sqsize;
  268.             ymax=y+sqsize;
  269.             if (xmax>=Width) xmax=Width-1;
  270.             if (ymax>=Height) ymax=Height-1;
  271.  
  272.             /* Draw the square */
  273.             RectFill(RP,xmin,ymin,xmax,ymax);
  274.  
  275.                 if (count%5==0) {
  276.  
  277.                RetVal = ContinueBlanking();
  278.                if (RetVal!=OK) break;
  279.  
  280.                   if (count%50==0) {
  281.                   ScreenToFront( Scr );
  282.                   if (clock()>zeit+duration) {
  283.  
  284.                      zeit=clock();
  285.                      /* whole screen in same color ? */
  286.  
  287.                      if (blackwin>0) color=black;
  288.                         else color=ReadPixel(RP,0,0);
  289.  
  290.                      /* four quadrants */
  291.  
  292.                      RetVal=OK;
  293.  
  294.                      if (Recheckt(RP,0,0,Width/2,Height/2,color)==1) {
  295.                         RetVal = ContinueBlanking();
  296.                         if (RetVal!=OK) break;
  297.                         if (Recheckt(RP,Width/2+1,0,Width-1,Height/2,color)==1) {
  298.                            RetVal = ContinueBlanking();
  299.                            if (RetVal!=OK) break;
  300.                            if (Recheckt(RP,0,Height/2+1,Width/2,Height-1,color)==1) {
  301.                               RetVal = ContinueBlanking();
  302.                               if (RetVal!=OK) break;
  303.                               if (Recheckt(RP,Width/2+1,Height/2+1,Width-1,Height-1,color)==1) {
  304.                                  RetVal = ContinueBlanking();
  305.                                  if (RetVal!=OK) break;
  306.                                  RetVal = -42;
  307.                                  break;
  308.                               }
  309.                            }
  310.                         }
  311.                      }
  312.                   }
  313.                }
  314.             }
  315.          }
  316.  
  317.          UnblankMousePointer( Wnd );
  318.          CloseScreen( Scr );
  319.       }
  320.       else RetVal=FAILED;
  321.  
  322.       if (RetVal!=-42) break;
  323.  
  324.    }
  325.  
  326.    return RetVal;
  327. }
  328.